//
//  SWMusicPlayerCollectionCell.swift
//  ChildStory
//
//  Created by linhan on 2017/1/4.
//  Copyright © 2017年 Aiya. All rights reserved.
//

import Foundation
import UIKit
class SWMusicPlayerCollectionCell: UICollectionViewCell
{
    weak var music:SWMusic? {
        didSet {
            if _inited && _change
            {
                layoutSubviews()
            }
        }
    }
    
    var loading:Bool = false{
        didSet{
            loading ? _loadingView?.startAnimating() : _loadingView?.stopAnimating()
        }
    }
    
    var playing:Bool = false{
        didSet{
            if playing
            {
                //avatarViewRotate()
                clearInterval(_intervalID)
                _intervalID = setInterval(0.1, closure: {[weak self] in
                    self?.avatarViewRotate()
                })
                
            }
            else
            {
                //_avatarView.layer.removeAllAnimations()
                clearInterval(_intervalID)
            }
        }
    }
    
    private var _change:Bool = true
    private var _inited:Bool = false
    private var _intervalID:String = ""
    
    private var _loadingView:SWLoadingView?
    lazy private var _avatarView:UIImageView = UIImageView(image:UIImage(named:"music_img_avatar"))
    
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        setup()
    }
    
    deinit
    {
        clearInterval(_intervalID)
        trace("DEINIT SWMusicPlayerCollectionCell")
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setup()
    {
        backgroundColor = UIColor.clear
        
        _avatarView.center = bounds.center
        contentView.addSubview(_avatarView)
        
        let loadingView = SWLoadingView(frame:_avatarView.frame)
        loadingView.isHidden = true
        contentView.addSubview(loadingView)
        _loadingView = loadingView
        
        _inited = true
    }
    
    func reset()
    {
        trace("SWMusicPlayerCollectionCell reset")
        loading = false
        playing = false
        _avatarView.transform = CGAffineTransform.identity
    }
    
    private func avatarViewRotate()
    {
//        UIView.animate(withDuration: 6, delay: 0, options: [.curveLinear], animations: {
//            //无法以动画形式旋转360°，只能转完180°再转180°
//            self._avatarView.transform = self._avatarView.transform.rotated(by: CGFloat(M_PI))
//        }, completion: {finish in
//            if self.playing
//            {
//                self.avatarViewRotate()
//            }
//        })
        _avatarView.transform = self._avatarView.transform.rotated(by: 0.01)
    }
    
    override func prepareForReuse()
    {
        super.prepareForReuse()
        _change = true
    }
    
    override func layoutSubviews()
    {
        super.layoutSubviews()
        
        if let music = music , _change
        {
            _change = false
            
        }
    }
    
}


fileprivate class SWLoadingView:UIView
{
    private var _indicatorView = UIActivityIndicatorView(activityIndicatorStyle: .gray)
    
    override init(frame:CGRect)
    {
        super.init(frame:frame)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func stopAnimating()
    {
        isHidden = true
        _indicatorView.stopAnimating()
    }
    
    func startAnimating()
    {
        isHidden = false
        _indicatorView.startAnimating()
    }
    
    private func setup()
    {
        backgroundColor = UIColor(white: 1, alpha: 0.5)
        layer.cornerRadius = bounds.width * 0.5
        layer.masksToBounds = true
        
        _indicatorView.center = bounds.center
        addSubview(_indicatorView)
    }
    
}
